package top.codedance.iotp.tools.terminal.ui;

import top.codedance.iotp.tools.terminal.TerminalApplication;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.net.URL;

/**
 *
 * @author xuedianfu
 */
public class LogManage extends javax.swing.JFrame {

    private ShellFrame sf;

    /** Creates new form LogManage */
    public LogManage(ShellFrame sf) {
        this.sf = sf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.sf);
        setMinimumSize(new Dimension(600, 400));
        setTitle("代码飞舞采控系统-日志管理");

        intLog();
    }

    private void intLog(){
        jta_log.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                initPos();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                initPos();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                initPos();
            }
        });
        new Thread(() -> {
            while (true) {
                jta_log.setText(TerminalApplication.readLog());
                initPos();
                try {
                    Thread.sleep(1000);
                }catch (Exception e){
                    TerminalApplication.syslogInfo(e.getMessage());
                }
            }
        }).start();
    }

    private void initPos() {
        int pos = jta_log.getDocument().getLength();
        jta_log.setCaretPosition(pos);
    }

    public void append(String data){
        jta_log.append(data);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jta_log = new javax.swing.JTextArea();
        jb_clean = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jta_log.setEditable(false);
        jta_log.setColumns(20);
        jta_log.setRows(5);
        jScrollPane1.setViewportView(jta_log);

        jb_clean.setText("清空");
        jb_clean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_cleanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jb_clean)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_clean)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_cleanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_cleanActionPerformed
        jta_log.setText("");
        TerminalApplication.writeLog(jta_log.getText());
    }//GEN-LAST:event_jb_cleanActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jb_clean;
    private javax.swing.JTextArea jta_log;
    // End of variables declaration//GEN-END:variables

}
